package com.ly.lypopupwindowdemo.views;

import android.content.Context;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.ly.lypopupwindowdemo.R;

/**
 * Created by liying on 2017/5/18.
 */

public class MyListView extends ListView implements View.OnTouchListener,GestureDetector.OnGestureListener {

    private GestureDetector gestureDetector;
    private View deleteButton;
    private OnDeleteListener listener;
    private ViewGroup itemLayout;
    private int selectedItem;
    private boolean isDeleteShown;

    public MyListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        gestureDetector = new GestureDetector(getContext(),this);
        setOnTouchListener(this);
    }

    public void setOnDeleteListener(OnDeleteListener l){
        listener = l;
    }
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (isDeleteShown){
            itemLayout.removeView(deleteButton);
            deleteButton = null;
            isDeleteShown = false;
            return false;
        }else{
            return gestureDetector.onTouchEvent(event);
        }
    }

    /**
     * 用户轻触触摸屏，由1个MotionEvent ACTION_DOWN触发
     * @param e
     * @return
     */

    @Override
    public boolean onDown(MotionEvent e) {
        //手指按下通过pointToPosition()方法判断当前选中的是ListView的哪一行。
        //pointToPosition是listview中的方法
        //当手指快速滑动的时候回调用onFling()方法。

        if (!isDeleteShown){
            selectedItem = pointToPosition((int) e.getX(),(int) e.getY());
        }
        return false;
    }

    /**
     * 用户轻触触摸屏，尚未松开或拖动，由一个1个MotionEvent ACTION_DOWN触发
     * 注意和onDown()的区别，强调的是没有松开或者拖动的状态
     * @param e
     */
    @Override
    public void onShowPress(MotionEvent e) {

    }

    /**
     * 用户（轻触触摸屏后）松开，由一个1个MotionEvent ACTION_UP触发
     * @param e
     * @return
     */
    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        return false;
    }

    /**
     * 用户按下触摸屏，并拖动，由1个MotionEvent ACTION_DOWN, 多个ACTION_MOVE触发
     * @param e1
     * @param e2
     * @param distanceX
     * @param distanceY
     * @return
     */

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        return false;
    }

    /**
     *  用户长按触摸屏，由多个MotionEvent ACTION_DOWN触发
     * @param e
     */
    @Override
    public void onLongPress(MotionEvent e) {

    }

    /**
     * 用户按下触摸屏、快速移动后松开，由1个MotionEvent ACTION_DOWN, 多个ACTION_MOVE, 1个ACTION_UP触发
     * @param e1
     * @param e2
     * @param velocityX
     * @param velocityY
     * @return
     */

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        /**
         * onFling()方法，在这里会去加载delete_button.xml这个布局，
         * 然后将删除按钮添加到当前选中的那一行item上。注意，我们还给删除
         * 按钮添加了一个点击事件，当点击了删除按钮时就会回调onDeleteListener
         * 的onDelete()方法，在回调方法中应该去处理具体的删除操作。
         */
        if (!isDeleteShown && Math.abs(velocityX) > Math.abs(velocityY)) {
            deleteButton = LayoutInflater.from(getContext()).inflate(
                    R.layout.delete_button, null);
            deleteButton.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    itemLayout.removeView(deleteButton);
                    deleteButton = null;
                    isDeleteShown = false;
                    listener.onDelete(selectedItem);
                }
            });
            itemLayout = (ViewGroup) getChildAt(selectedItem
                    - getFirstVisiblePosition());
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            params.addRule(RelativeLayout.CENTER_VERTICAL);
            itemLayout.addView(deleteButton, params);
            isDeleteShown = true;
        }
        return false;
    }

    public interface OnDeleteListener{
        void onDelete(int index);
    }
}
